#include "debug.h"
#include "../DDIC/AllDDIC.h"
#include "usbd_cdc.h"



/********************************************************************************************
********************************************************************************************
    CH13721  写寄存器 读寄存器  写Gamma 读Gamma OTP烧录
    CH13721  写寄存器 读寄存器  写Gamma 读Gamma OTP烧录
    CH13721  写寄存器 读寄存器  写Gamma 读Gamma OTP烧录
    CH13721  写寄存器 读寄存器  写Gamma 读Gamma OTP烧录
    CH13721  写寄存器 读寄存器  写Gamma 读Gamma OTP烧录
********************************************************************************************
********************************************************************************************/
void CH13721_Write_Register(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
		if(USB_Rx_Buffer[1]&0x80)
			SSD2828_W_Reg(SigMode,channel,0xB7,(HS_B7_Data|0x0010)&0XFFFD);
		else
			SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);
    SSD2828_W_Array(SigMode,channel,buffer,2);                               

    delay_ms(5);
    SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
    buffer[4] = Uart_Error_None;
    buffer[5] = 0;
    STM2PC(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //返回 写寄存器状态  ：ok  
}
void CH13721_Write_51Register(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
    SSD2828_W_Reg(SigMode,channel,0xB7,0x0019);
	  //SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);
		delay_ms(5);
    SSD2828_W_Array(SigMode,channel,buffer,2);                               

    delay_ms(5);
    SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
    buffer[4] = Uart_Error_None;
    buffer[5] = 0;
    STM2PC(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //返回 写寄存器状态  ：ok  
}
void CH13721_Read_Register(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
   u16 tmp;
    buffer[1]=0x03;
    SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data|0x0080);                               
    SSD2828_W_Reg(SigMode,channel,0xC1,buffer[2]); //return package size                                
    SSD2828_W_Reg(SigMode,channel,0xC0,0x0001);
    buffer[0] = 0x01;
    buffer[1] = buffer[3];                        
    SSD2828_W_Array(SigMode,channel,buffer,0);
    delay_ms(5);  
    SSD2828_W_Reg(SigMode,channel,0xD4,0x00FA);
    SSD2828_W_Cmd(SigMode,channel,0xFF);
    for(i=0;i<buffer[2]-1;i++)
    {
        SSD2828_W_Cmd(SigMode,channel,0xFA);		
        tmp=SPI3_Read_u16_Data(channel);
        buffer[4+i]=tmp>>8;
        buffer[5+i]=tmp;
        delay_ms(5);
        i++;
    }
		buffer[0]=0x4A;	
		buffer[1]=0x02;
    SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
    STM2PC(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //返回 读取的 寄存器数据  ：ok 
}

void CH13721_Write_Gamma(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
    u8 buffer1[7];
    u8 i,j,m,k=0,temp;
	
	  SSD2828_W_Reg(SigMode,channel,0xB7,0x0019);
		delay_ms(5);
	   
	  
	  
	  if((buffer[3]&0xf0)==0x00)       //GAMMA RED
	  {
	     if((buffer[4]&0x03)==0x00)
		   {
				 SSD2828_W_Reg(SigMode,channel,0xBC,16); 
		     SSD2828_W_Cmd(SigMode,channel,0xBF);	
			   SSD2828_W_Data(SigMode,channel,0xB2); //GAMMA RED GROUP1
		   }
		   else if((buffer[4]&0x03)==0x01)
		   {
				 SSD2828_W_Reg(SigMode,channel,0xBC,16); 
		     SSD2828_W_Cmd(SigMode,channel,0xBF);	
			   SSD2828_W_Data(SigMode,channel,0xB3); //GAMMA RED GROUP2
		   }
		   else if((buffer[4]&0x03)==0x02)
		   {
				 SSD2828_W_Reg(SigMode,channel,0xBC,14); 
		     SSD2828_W_Cmd(SigMode,channel,0xBF);	
			   SSD2828_W_Data(SigMode,channel,0xB4); //GAMMA RED GROUP3
		   }
	  }
		else if((buffer[3]&0xf0)==0x40)  //GAMMA GREEN
		{
			if((buffer[4]&0x03)==0x00)
		   {
				 SSD2828_W_Reg(SigMode,channel,0xBC,16); 
		     SSD2828_W_Cmd(SigMode,channel,0xBF);	
			   SSD2828_W_Data(SigMode,channel,0xB5); //GAMMA GREEN GROUP1
		   }
		   else if((buffer[4]&0x03)==0x01)
		   {
				 SSD2828_W_Reg(SigMode,channel,0xBC,16); 
		     SSD2828_W_Cmd(SigMode,channel,0xBF);	
			   SSD2828_W_Data(SigMode,channel,0xB6); //GAMMA GREEN GROUP2
		   }
		   else if((buffer[4]&0x03)==0x02)
		   {
				 SSD2828_W_Reg(SigMode,channel,0xBC,14); 
		     SSD2828_W_Cmd(SigMode,channel,0xBF);	
			   SSD2828_W_Data(SigMode,channel,0xB7); //GAMMA GREEN GROUP3
		   }
		}
		else if((buffer[3]&0xf0)==0x80)   //GAMMA BLUE
		{
			if((buffer[4]&0x03)==0x00)
		   {
				 SSD2828_W_Reg(SigMode,channel,0xBC,16); 
		     SSD2828_W_Cmd(SigMode,channel,0xBF);	
			   SSD2828_W_Data(SigMode,channel,0xB8); //GAMMA BLUE GROUP1
		   }
		   else if((buffer[4]&0x03)==0x01)
		   {
				 SSD2828_W_Reg(SigMode,channel,0xBC,16); 
		     SSD2828_W_Cmd(SigMode,channel,0xBF);	
			   SSD2828_W_Data(SigMode,channel,0xB9); //GAMMA BLUE GROUP2
		   }
		   else if((buffer[4]&0x03)==0x02)
		   {
				 SSD2828_W_Reg(SigMode,channel,0xBC,14); 
		     SSD2828_W_Cmd(SigMode,channel,0xBF);	
			   SSD2828_W_Data(SigMode,channel,0xBA); //GAMMA BLUE GROUP3
		   }
		} 
    
					
		if(((buffer[4]&0x03)==0x00)||((buffer[4]&0x03)==0x01))
		{
		   for( i = 0;i<15;i++)   //15个绑点
		   {	                                                                                  
			   SSD2828_W_Data(SigMode,channel,buffer[i+5]);	
			   delay_ms(5);  
		   }
	  }
		else if((buffer[4]&0x03)==0x02)
		{
			for( i = 0;i<13;i++)   //13个绑点
		   {	                                                                                  
			   SSD2828_W_Data(SigMode,channel,buffer[i+5]);	
			   delay_ms(5);  
		   }
		}
		
		buffer1[0] = 0x02;        
    buffer1[1] = 0xBE;
    buffer1[2] = 0x01;    
    SSD2828_W_Array(SigMode,channel,buffer1,0);		 
	  delay_ms(5); 
				
    buffer[4] = Uart_Error_None;
    buffer[5] = 0;
    STM2PC(pdev,CDC_IN_EP,buffer,buffer[2]+3);            //返回 写寄存器状态  ：ok  
	  SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
}

void CH13721_Read_Gamma(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
    u8 buffer1[7];
    u16 tmp;
    u8 i,j,k=0;
		
		 if((buffer[3]&0xf0)==0x00)       //GAMMA RED
	  {
	     if((buffer[4]&0x03)==0x00)
		   {
				 SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data|0x0080);
	       SSD2828_W_Reg(SigMode,channel,0xC1,0x000F); //返回个数设置
		     SSD2828_W_Reg(SigMode,channel,0xC0,0x0001); 
		     delay_ms(10);
				 
			   buffer1[0] = 0x02;        
         buffer1[1] = 0xB2;          
         SSD2828_W_Array(SigMode,channel,buffer1,0);		 
	       delay_ms(5);                 //GAMMA RED GROUP1
		   }
		   else if((buffer[4]&0x03)==0x01)
		   {
				 SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data|0x0080);
	       SSD2828_W_Reg(SigMode,channel,0xC1,0x000F); //返回个数设置
		     SSD2828_W_Reg(SigMode,channel,0xC0,0x0001); 
		     delay_ms(10);
				 
			   buffer1[0] = 0x02;        
         buffer1[1] = 0xB3;          
         SSD2828_W_Array(SigMode,channel,buffer1,0);		 
	       delay_ms(5);  
		   }
		   else if((buffer[4]&0x03)==0x02)
		   {
				 SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data|0x0080);
	       SSD2828_W_Reg(SigMode,channel,0xC1,0x000D); //返回个数设置
		     SSD2828_W_Reg(SigMode,channel,0xC0,0x0001); 
		     delay_ms(10);
				 
			   buffer1[0] = 0x02;        
         buffer1[1] = 0xB4;          
         SSD2828_W_Array(SigMode,channel,buffer1,0);		 
	       delay_ms(5);  
		   }
	  }
		else if((buffer[3]&0xf0)==0x40)  //GAMMA GREEN
		{
			if((buffer[4]&0x03)==0x00)
		   {
			  SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data|0x0080);
	       SSD2828_W_Reg(SigMode,channel,0xC1,0x000F); //返回个数设置
		     SSD2828_W_Reg(SigMode,channel,0xC0,0x0001); 
		     delay_ms(10);
				 
			   buffer1[0] = 0x02;        
         buffer1[1] = 0xB5;          
         SSD2828_W_Array(SigMode,channel,buffer1,0);		 
	       delay_ms(5);                 //GAMMA RED GROUP1
		   }
		   else if((buffer[4]&0x03)==0x01)
		   {
			   SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data|0x0080);
	       SSD2828_W_Reg(SigMode,channel,0xC1,0x000F); //返回个数设置
		     SSD2828_W_Reg(SigMode,channel,0xC0,0x0001); 
		     delay_ms(10);
				 
			   buffer1[0] = 0x02;        
         buffer1[1] = 0xB6;          
         SSD2828_W_Array(SigMode,channel,buffer1,0);		 
	       delay_ms(5);                 //GAMMA RED GROUP1
		   }
		   else if((buffer[4]&0x03)==0x02)
		   {
			   SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data|0x0080);
	       SSD2828_W_Reg(SigMode,channel,0xC1,0x000D); //返回个数设置
		     SSD2828_W_Reg(SigMode,channel,0xC0,0x0001); 
		     delay_ms(10);
				 
			   buffer1[0] = 0x02;        
         buffer1[1] = 0xB7;          
         SSD2828_W_Array(SigMode,channel,buffer1,0);		 
	       delay_ms(5);  
		   }
		}
		else if((buffer[3]&0xf0)==0x80)   //GAMMA BLUE
		{
			if((buffer[4]&0x03)==0x00)
		   {
			  SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data|0x0080);
	       SSD2828_W_Reg(SigMode,channel,0xC1,0x000F); //返回个数设置
		     SSD2828_W_Reg(SigMode,channel,0xC0,0x0001); 
		     delay_ms(10);
				 
			   buffer1[0] = 0x02;        
         buffer1[1] = 0xB8;          
         SSD2828_W_Array(SigMode,channel,buffer1,0);		 
	       delay_ms(5);                 //GAMMA RED GROUP1 
		   }
		   else if((buffer[4]&0x03)==0x01)
		   {
			 SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data|0x0080);
	       SSD2828_W_Reg(SigMode,channel,0xC1,0x000F); //返回个数设置
		     SSD2828_W_Reg(SigMode,channel,0xC0,0x0001); 
		     delay_ms(10);
				 
			   buffer1[0] = 0x02;        
         buffer1[1] = 0xB9;          
         SSD2828_W_Array(SigMode,channel,buffer1,0);		 
	       delay_ms(5);                 //GAMMA RED GROUP1
		   }
		   else if((buffer[4]&0x03)==0x02)
		   {
			   SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data|0x0080);
	       SSD2828_W_Reg(SigMode,channel,0xC1,0x000D); //返回个数设置
		     SSD2828_W_Reg(SigMode,channel,0xC0,0x0001); 
		     delay_ms(10);
				 
			   buffer1[0] = 0x02;        
         buffer1[1] = 0xBA;          
         SSD2828_W_Array(SigMode,channel,buffer1,0);		 
	       delay_ms(5);  
		   }
		} 
		
		SSD2828_W_Reg(SigMode,channel,0xD4,0x00FA);
		SSD2828_W_Cmd(SigMode,channel,0xFF);
		delay_ms(5);
		
		if(((buffer[4]&0x03)==0x00)||((buffer[4]&0x03)==0x01))
		{
			    for(i=0;i<15;i++)
		  {
				  SSD2828_W_Cmd(SigMode,channel,0xFA);		
				  tmp=SPI3_Read_u16_Data(channel);
				  buffer[4+i]=tmp>>8;
				  buffer[5+i]=tmp;
			    delay_ms(5);
				  i++;			
		  }
		}
		else if((buffer[4]&0x03)==0x02)
		{
			for(i=0;i<13;i++)
		  {
				  SSD2828_W_Cmd(SigMode,channel,0xFA);		
				  tmp=SPI3_Read_u16_Data(channel);
				  buffer[4+i]=tmp>>8;
				  buffer[5+i]=tmp;
			    delay_ms(5);
				  i++;			
		  }
		}
		
    buffer[0]=0x4A;	//CH13721
    buffer[1]=0x09;
		delay_ms(15);
    STM2PC(pdev,CDC_IN_EP,buffer,buffer[2]+3);          //返回 读取的 Gamma数据  ：ok  		

    SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
}

void CH13721_Gamma_OTP_Start(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
    u8 buffer1[10];
	  u8 mtp_flag;
	  SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);
		delay_ms(15); 
    buffer1[0] = 0x02;
    buffer1[1] = 0xEB;
    buffer1[2] = 0x10;         //OTP_Register_Access_Enable                      
    SSD2828_W_Array(SigMode,channel,buffer1,0); 
		delay_ms(100); 
	
	 if(buffer[1]==0x0A)  //otp Gamma
	 {
		 buffer1[0] = 0x02;
     buffer1[1] = 0xED;
     buffer1[2] = 0x40;  //OTP烧录项
		 SSD2828_W_Array(SigMode,channel,buffer1,0); 
		delay_ms(10); 
	 }
	 else if(buffer[1]==0x0D)  //otp HBM
	 {
		 buffer1[0] = 0x02;
     buffer1[1] = 0xED;
     buffer1[2] = 0x20;  //OTP烧录项
		 SSD2828_W_Array(SigMode,channel,buffer1,0); 
		delay_ms(10); 
	 }
    else if(buffer[1]==0x0E)  //otp GOA
	 {
		 buffer1[0] = 0x02;
     buffer1[1] = 0xED;
     buffer1[2] = 0x02;  //OTP烧录项
		 SSD2828_W_Array(SigMode,channel,buffer1,0); 
		delay_ms(10); 
	 }
   else if(buffer[1]==0x0E)  //otp ID
	 {
		 buffer1[0] = 0x02;
     buffer1[1] = 0xED;
     buffer1[2] = 0x80;  //OTP烧录项
		 SSD2828_W_Array(SigMode,channel,buffer1,0); 
		delay_ms(10); 
	 }
	 
    buffer1[0] = 0x04;
    buffer1[1] = 0xEE;
    buffer1[2] = 0xA5;    //OTP_MODE_ACTIVE
	  buffer1[3] = 0x5A;    //OTP_MODE_ACTIVE
	  buffer1[4] = 0x3C;    //OTP_MODE_ACTIVE
		SSD2828_W_Array(SigMode,channel,buffer1,0);	
		delay_ms(500); 	
	
     buffer1[0] = 0x02;
     buffer1[1] = 0xED;
     buffer1[2] = 0x00;  //
		 SSD2828_W_Array(SigMode,channel,buffer1,0); 
		delay_ms(10);     	
		
		buffer[1] = 0x0A;
		buffer[2] = 0x02;
		buffer[3] = Uart_Error_None;   //返回 OTP Gamma数据  ：OK
		buffer[4] = 0;
		STM2PC(pdev,CDC_IN_EP,buffer,buffer[2]+3);         
}


void CH13721_Read_OTP_Times(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
    u16 tmp;
    buffer[1]=0x03;
    SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data|0x0080);                               
    SSD2828_W_Reg(SigMode,channel,0xC1,0x0001); //return package size                                
    SSD2828_W_Reg(SigMode,channel,0xC0,0x0001);
    buffer[0] = 0x01;
    buffer[1] = 0xD8;                        
    SSD2828_W_Array(SigMode,channel,buffer,0);
    delay_ms(5);  
		SSD2828_W_Reg(SigMode,channel,0xD4,0x00FA);
    SSD2828_W_Cmd(SigMode,channel,0xFF);

		SSD2828_W_Cmd(SigMode,channel,0xFA);		
		buffer[4]=SPI3_Read_u8_Data(channel);
		delay_ms(5);

		buffer[0]=0x2D;	
		buffer[1]=0x0B;
    SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
	
    STM2PC(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //返回 读取的 寄存器数据  ：ok 
}


void ProcessForIc4A( USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8 buffer[],u16 LP_B7_Data,u16 HS_B7_Data)
{	
	//void *pdev;
	switch(buffer[1]&0x0F)
	{
			case 0x01:                                      //写寄存器
				CH13721_Write_Register(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);break;	
			case 0x02:                                      //读寄存器
				CH13721_Read_Register(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);break;	
			case 0x03:                                      //写寄存器
				CH13721_Write_51Register(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);break;															
			case 0x08:                                      //写Gamma数据    
				CH13721_Write_Gamma(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);break;
			case 0x09:                                      //读Gamma数据
				CH13721_Read_Gamma(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);break; 
			case 0x0A://gamma otp
										case 0x0D://OTP HBM
			case 0x0E://OTP GOA
			case 0x0F://OTP ID 	
				CH13721_Gamma_OTP_Start(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);  break;
			case 0x0B://OTP times
				CH13721_Read_OTP_Times(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);  break;							
			default:	break;	
	}			
}


